package com.pignest.hotel.ui.member;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.Memberchargehistrory;
import com.pignest.hotel.definition.ChargeOpType;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class ChargeWindow extends Window implements EventListener {

	/**
	 * 会员充值
	 */
	private static final long serialVersionUID = -5854113353799282624L;
	private MemberService memberService;
	private Member member;
	
	private Textbox priceTextbox;
	
	private Listbox chargeTypeListbox;
	
	private Textbox remarkTextbox;
	private Label namelabel;
	private Label levelLabel;
	private Label mobileLabel;
	private Label sexLabel;
	private Label memberTypeLabel;
	private Label stateLabel;
	private Label registModeLabel;
	private Label creditLabel;
	private Label integralLabel;
	private Label balanceLabel;
	private Listbox chargeListbox;
	public ChargeWindow(){
		memberService = Services.getMemberService();
		addEventListener("onCreate", this);
		
	}
	
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			namelabel=(Label)getFellow("name");
			levelLabel=(Label)getFellow("level");
			mobileLabel =(Label)getFellow("mobile");
			sexLabel=(Label)getFellow("sex");
			memberTypeLabel=(Label)getFellow("memberType");
			stateLabel=(Label)getFellow("state");
			registModeLabel=(Label)getFellow("registMode");
			creditLabel =(Label)getFellow("credit");
			integralLabel=(Label)getFellow("integral");
			balanceLabel=(Label)getFellow("balance");
			priceTextbox=(Textbox)getFellow("priceTextbox");
			chargeTypeListbox=(Listbox)getFellow("chargeTypeListbox");
			remarkTextbox=(Textbox)getFellow("remarkTextbox");
			
			chargeListbox=(Listbox)getFellow("chargeListbox");
			
			setValue();
			queryChargeList();
		}
	}
	public void setValue(){
		if(this.member!=null){
		   namelabel.setValue(this.member.getName());
		   if(this.member.getMemberLevel()!=null)
		       levelLabel.setValue(this.member.getMemberLevel().getMemberlevel());
		   else
			   levelLabel.setValue("");
		   mobileLabel.setValue(this.member.getMobile());
		   sexLabel.setValue(this.member.getSex());
		   memberTypeLabel.setValue(this.member.getType());
		   stateLabel.setValue(memberService.queryCardState(member));
		   registModeLabel.setValue(this.member.getRegisterMode());
		   Integer credit =this.member.getCredit();
		   if(credit!=null)
		      creditLabel.setValue(credit.toString());
		   Integer intgral =this.member.getIntegral();
		   if(intgral!=null)
		     integralLabel.setValue(intgral.toString());
		   Integer balance= this.member.getBalance();
		   if(balance!=null)
		     balanceLabel.setValue(MoneyUtil.toString(balance));
		   
		}
	}
	public void charge(){
		try{
            if(this.member==null){
            	UIUtils.alert("无效的会员！");
            	return ;
            }
            if(!Hotel.onDuty()){
            	UIUtils.alert("未在值班不能操作充值业务！");
            	return ;
            }
			String price =priceTextbox.getValue();
			int iPrice =Utils.toPrice(price);
			if(!Utils.checkPrice(price))
			{
				UIUtils.alert("请输入合法值，非负数,小数不能超过2位");
				return ;
			}
			
			if(iPrice==0){
				UIUtils.alert("充值金额不能为0");
				return ;
			}
			
			String chargetype =UIUtils.getSelectedValue(chargeTypeListbox);
			String remark =remarkTextbox.getValue();
			Memberchargehistrory ms = new Memberchargehistrory();
			ms.setMember(this.member);
			ms.setChargeTime(new Date());
			ms.setChargetype(chargetype);
			ms.setPrice(Utils.toPrice(price));
			ms.setOptype(ChargeOpType.OnCharge.name());
			ms.setRemark(remark);
			this.member=memberService.chargeMember(ms);
			balanceLabel.setValue(MoneyUtil.toString(this.member.getBalance()));
			queryChargeList();

		}catch(Exception e){
			e.printStackTrace();
		}
	}
    public void setMeber(String id){
    	Member member = memberService.getMember(id);
    	this.member=member;
    }
    public void queryChargeList(){
    	chargeListbox.getItems().clear();
    	List <Memberchargehistrory> history=memberService.queryHistory(this.member);
    	for(Memberchargehistrory h:history){
    		Listitem listitem = new Listitem();
	    	listitem.setParent(chargeListbox);
	    	listitem.appendChild(new Listcell(h.getChargetype()));
	    	listitem.appendChild(new Listcell(ChargeOpType.valueOf(h.getOptype()).getValue()));
	    	listitem.appendChild(new Listcell(MoneyUtil.toString(h.getPrice())));
	    	listitem.appendChild(new Listcell(DateUtil.toFullString(h.getChargeTime())));
	    	listitem.appendChild(new Listcell(h.getRemark()));
    	}
    	
    }
}
